﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for PanelBusiness
/// </summary>
public class PanelBusiness
{
    private PanelData panel = new PanelData();
	public PanelBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool add(PanelesEntities item)
    {
        return panel.add(item);
    }
    public bool edit(PanelesEntities item)
    {
        return panel.edit(item);
    }
    public bool delete(int id)
    {
        return panel.delete(id);
    }

    public PanelesEntities getItem(int id)
    {
        PanelesEntities item = null;
        DataTable dt = panel.getPanel(id);
        if (dt != null)
        {
                DataRow row = dt.Rows[0];
                item = new PanelesEntities();
                item._id = int.Parse(row["id"].ToString().Trim());
                item._name = row["name"].ToString();
                item._channelID = int.Parse(row["channel_id"].ToString().Trim());
                item._control = row["control_url"].ToString();
                item._cellID = int.Parse(row["cell_id"].ToString().Trim());
                item._priority = int.Parse(row["priority"].ToString().Trim());
                item._category = int.Parse(row["category"].ToString().Trim());
                item._displayAll = row["display_all"].ToString();
                item._displayContainer = row["display_container"].ToString();
                item._file1 = int.Parse(row["file1"].ToString().Trim());
                item._file2 = int.Parse(row["file2"].ToString().Trim());
        }
        return item;
    }

    public System.Collections.ArrayList getPanel()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        DataTable dt = panel.getPanel(0);
        if (dt != null)
        {
            PanelesEntities item = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                item = new PanelesEntities();
                item._id = int.Parse(row["id"].ToString().Trim());
                item._name = row["name"].ToString();
                item._channelID = int.Parse(row["channel_id"].ToString().Trim());
                item._control = row["control_url"].ToString();
                item._cellID = int.Parse(row["cell_id"].ToString().Trim());
                item._priority = int.Parse(row["priority"].ToString().Trim());
                item._category = int.Parse(row["category"].ToString().Trim());
                item._displayAll = row["display_all"].ToString();
                item._displayContainer = row["display_container"].ToString();
                item._file1 = int.Parse(row["file1"].ToString().Trim());
                item._file2 = int.Parse(row["file2"].ToString().Trim());
                items.Add(item);
                item = null;
            }
        }
        return items;
    }
    public string[,] getCell_All()
    {
        System.Collections.ArrayList tmp = new System.Collections.ArrayList();
        System.Collections.ArrayList tmp1 = new System.Collections.ArrayList();
        string[,] cell = Option.cell;
        System.Collections.ArrayList items = this.getPanel_All();
            for (int i = 0; i < cell.Length/2; i++)
            {
                bool flat = false;
                if (items != null && items.Count > 0)
                {
                    PanelesEntities item = null;
                    for (int j = 0; j < items.Count; j++)
                    {
                        item = (PanelesEntities)items[j];
                        if (item._cellID.ToString().Equals(cell[i,0].ToString()))//o da duoc hien thi all trang
                        {
                            flat = true;
                            item = null;
                            break;
                        }
                        item = null;
                    }
                }

                if (!flat)
                {
                    tmp1.Add(cell[i, 0].ToString());//key
                    tmp.Add(cell[i, 1].ToString());//view
                }
                   
            }

            int l = tmp.Count;
        string[,] cell_tmp = new string[l,2];
        for (int i = 0; i < l; i++)
        {
            cell_tmp[i, 0] = (string)tmp1[i];
            cell_tmp[i,1] = (string)tmp[i];
        }
            return cell_tmp;
    }
    public System.Collections.ArrayList getPanel_All()
    {
        System.Collections.ArrayList tmp = new System.Collections.ArrayList();
        System.Collections.ArrayList items = this.getPanel();
        if (items != null)
        {
            PanelesEntities item = null;
            for (int i = 0; i < items.Count; i++)
            {
                item = (PanelesEntities)items[i];
                if (item._displayAll.Trim().Equals("True") || item._displayAll.Trim().Equals("1"))
                {
                    tmp.Add(item);
                }
                item = null;
            }
        }
        return tmp;
    }

    public System.Collections.ArrayList getPanel_Category(int Panel_category)
    {
        System.Collections.ArrayList tmp = new System.Collections.ArrayList();
        System.Collections.ArrayList items = this.getPanel();
        if (items != null)
        {
            PanelesEntities item = null;
            for (int i = 0; i < items.Count; i++)
            {
                item = (PanelesEntities)items[i];
                if (item._category == Panel_category)
                {
                    tmp.Add(item);
                }
                item = null;
            }
        }
        return tmp;
    }

    public System.Collections.ArrayList getPanel_Chanel(int channel_id)
    {
        System.Collections.ArrayList tmp = new System.Collections.ArrayList();
        System.Collections.ArrayList items = this.getPanel();
        if (items != null)
        {
            PanelesEntities item = null;
            for (int i = 0; i < items.Count; i++)
            {
                item = (PanelesEntities)items[i];
                if (item._channelID == channel_id)
                {
                    tmp.Add(item);
                }
                item = null;
            }
        }
        return tmp;
    }
}
